package com.base.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet; 
import java.util.Map;

import javax.ejb.Stateless;

import com.base.entity.TB_BASE_PROVINCES;
import com.base.entity.TB_BASE_STATE;
import com.base.entity.TB_BASE_STATEMENT;
import com.common.util.ObjectUtils;
import com.common.util.PinyinUtil;
import com.core.service.AppBaseService; 

/**
 * Session Bean implementation class SVC_BASE
 */
@Stateless
public class SVC_BASE extends AppBaseService implements SVC_BASERemote, SVC_BASELocal {

	@Override
	public Map ADD_STATEMENT(Map param) {
		TB_BASE_STATEMENT dto =  ObjectUtils.mapToJavaBean(param, TB_BASE_STATEMENT.class);
		dto.setID(dto.uuid());
		dto.setOP_ITEM(getOpTime(param));
		dto.setOPID(getOpid(param));
		getJpaDao().save(dto);
		return ObjectUtils.javaBeanToMap(dto);
	}

	@Override
	public Map UPD_STATEMENT(Map param) {
		TB_BASE_STATEMENT dto =  ObjectUtils.mapToJavaBean(param, TB_BASE_STATEMENT.class);
		dto.setOP_ITEM(getOpTime(param));
		dto.setOPID(getOpid(param));
		if(dto.getID()==null || dto.getID().isEmpty())return param;
		TB_BASE_STATEMENT po = getJpaDao().find(TB_BASE_STATEMENT.class, dto.getID()); 
		ObjectUtils.copyJavaBeanToBean(dto, po, "ID");
		getJpaDao().update(po);
		return  ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_STATEMENT(Map param) {
		String id = ObjectUtils.getMapValue(param, "ID", String.class);
		TB_BASE_STATEMENT po = getJpaDao().find(TB_BASE_STATEMENT.class, id); 
		if(id==null || id.isEmpty())return  ;
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_BASE_STATEMENT.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Collection<Map>  getRunningStatement() {
		Map cache = getStatementCache().getStatementCache();
		HashSet<Map> set = new HashSet<Map>();
		for(Object key:cache.keySet()){
			Object value = cache.get(key);
			HashMap item= new HashMap();
			item.put("KEY", key);
			item.put("VALUE", value);
			set.add(item);
		}
		return set;
	}

	@Override
	public Map ADD_STATE(Map param) { 
		String name = (String) param.get("NAME");
		String SHORTNAME = (String) param.get("SHORTNAME");
		if(SHORTNAME==null||SHORTNAME.isEmpty()){
			SHORTNAME = name;
		}
		param.put("PINYIN",PinyinUtil.getHeadByString(SHORTNAME));
		param.put("FULL_PINYIN",PinyinUtil.stringToPinyin(SHORTNAME));
		return ADD(TB_BASE_STATE.class,param);
	}

	@Override
	public Map UPD_STATE(Map param) { 
		String name = (String) param.get("NAME");
		String SHORTNAME = (String) param.get("SHORTNAME");
		if(SHORTNAME==null||SHORTNAME.isEmpty()){
			SHORTNAME = name;
		}
		param.put("PINYIN",PinyinUtil.getHeadByString(SHORTNAME));
		param.put("FULL_PINYIN",PinyinUtil.stringToPinyin(SHORTNAME));
		return UPD(TB_BASE_STATE.class,param);
	}

	@Override
	public void DEL_STATE(Map param) {
		DEL(TB_BASE_STATE.class,param);
	}

	@Override
	public Map ADD_PROVINCES(Map param) {
		String name = (String) param.get("NAME");
		String SHORTNAME = (String) param.get("SHORTNAME");
		if(SHORTNAME==null||SHORTNAME.isEmpty()){
			SHORTNAME = name;
		}
		param.put("PINYIN",PinyinUtil.getHeadByString(SHORTNAME));
		param.put("FULL_PINYIN",PinyinUtil.stringToPinyin(SHORTNAME));
		return ADD(TB_BASE_PROVINCES.class,param);
	}

	@Override
	public Map UPD_PROVINCES(Map param) {
		String name = (String) param.get("NAME");
		String SHORTNAME = (String) param.get("SHORTNAME");
		if(SHORTNAME==null||SHORTNAME.isEmpty()){
			SHORTNAME = name;
		}
		param.put("PINYIN",PinyinUtil.getHeadByString(SHORTNAME));
		param.put("FULL_PINYIN",PinyinUtil.stringToPinyin(SHORTNAME));
		return UPD(TB_BASE_PROVINCES.class,param);
	}

	@Override
	public void DEL_PROVINCES(Map param) {
		DEL(TB_BASE_PROVINCES.class,param);
	}
 

	@Override
	public void UPD_PROVINCES_STATUS(Map param) { 
		 int STATUS = (Integer) param.get("STATUS");
		TB_BASE_PROVINCES po = (TB_BASE_PROVINCES) querySingleResult("SELECT O FROM TB_BASE_PROVINCES O WHERE O.PROVINCE_CODE='"+param.get("PROVINCE_CODE")+"'");
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(STATUS);
		getJpaDao().update(po);
	} 
   
}
